package io.niguang.config.mybatis;

import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.io.File;
import java.util.ArrayList;

/**
 * <p>
 * 测试生成代码
 * </p>
 *
 * @author yhan219
 */
public class Generator {

    /**
     * 表前缀
     */
    public static final String TABLE_PREFIX = "work_";

    public static void main(String[] args) {
        String packageName = "io.niguang.modules.app";
        generateByTables(packageName, "salary_statistics");
    }


    public static void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://120.79.229.170:3306/work?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("Niguang@2018")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        StrategyConfig fillList = strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(false)
                .setDbColumnUnderline(true)
                .setRestControllerStyle(true)
                .setEntityColumnConstant(true)
                .setTablePrefix(TABLE_PREFIX)
                .setNaming(NamingStrategy.underline_to_camel)
                .setLogicDeleteFieldName("del_flag")
                .setTableFillList(new ArrayList<TableFill>() {{
                    add(new TableFill("create_time", FieldFill.INSERT));
                    add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
                }});
        if (tableNames != null) {
            fillList.setInclude(tableNames);
        }


        String srcPath = "src" + File.separator + "main" + File.separator + "java";
        config.setActiveRecord(true)
                .setAuthor(System.getProperty("user.name"))
                .setOutputDir(srcPath)
                .setEnableCache(false)
                .setBaseResultMap(true)
                .setBaseColumnList(false)
                .setKotlin(false)
                .setActiveRecord(true)
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                                .setModuleName("api")
                ).execute();
    }

    public static void generateByTables(String packageName, String... tableNames) {
        generateByTables(false, packageName, tableNames);
    }
}
